package com.back.service.impl;

import com.back.entity.Movie;
import com.back.entity.Review;
import com.back.entity.Score;
import com.back.mapper.MovieDao;
import com.back.mapper.ReviewDao;
import com.back.mapper.ScoreDao;
import com.back.mapper.UserDao;
import com.back.service.inte.ReviewService;
import com.back.uitls.MyException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ReviewServiceImpl implements ReviewService {

    @Autowired
    ReviewDao reviewDao;

    @Autowired
    ScoreDao scoreDao;

    @Autowired
    MovieDao movieDao;

    @Autowired
    UserDao userDao;

    /**
     * @param movie_id
     * @param offset
     * @param pagesize
     * @return
     */
    @Override
    public Map<String, Object> selectReviewByMovieId(int movie_id, int offset, int pagesize) throws MyException {
        if(movieDao.selectMovieById(movie_id) == null){
            throw new MyException("电影不存在");
        }
        if(offset < 1||pagesize < 1){
            throw new MyException("分页错误");
        }
        Map<String, Object> map = new HashMap<>();
        List<Review> reviews = reviewDao.selectReviewByMovieId(movie_id, (offset - 1) * pagesize, pagesize);
        List<Map<String, Object>> review_list = new ArrayList<>(reviews.size());

        for (Review review : reviews) {
            Map<String, Object> review_map = new HashMap<>();
            review_map.put("id", review.getId());
            review_map.put("movie_id", review.getMovie_id());
            review_map.put("from_user_id", review.getFrom_user_id());
            review_map.put("content", review.getContent());
            // 只要年月日秒
            review_map.put("date", review.getDate().toString().substring(0, 19));
            review_map.put("from_user_name", userDao.queryById(review.getFrom_user_id()).getName());

            review_list.add(review_map);
        }

        map.put("review_list", review_list);
        map.put("total", reviewDao.selectReviewCountByMovieId(movie_id));
        return map;
    }

    /**
     * @param review
     */
    @Override
    public void insertReview(Review review) throws MyException {

        if(userDao.queryById(review.getFrom_user_id()) == null){
            throw new MyException("用户不存在");
        }
        if(movieDao.selectMovieById(review.getMovie_id()) == null){
            throw new MyException("电影不存在");
        }
        if(review.getContent().length() > 255){
            throw new MyException("评论内容过长");
        }
        review.setDate(new java.sql.Timestamp(System.currentTimeMillis()));
        reviewDao.insertReview(review);
    }

    /**
     * @param movie_id
     * @param user_id
     * @param score
     */
    @Override
    public void insertScore(int movie_id, int user_id, int score) throws MyException {
        Movie movie = movieDao.selectMovieById(movie_id);
        if (movie == null) {
            throw new MyException("电影不存在");
        }
        if (score < 0 || score > 10) {
            throw new MyException("评分应在0-10之间");
        }
        if(user_id == 0){
            throw new MyException("请先登录");
        }
        // 该用户是否已经评分
        Score old_score = scoreDao.selectScoreByMovieIdAndUserId(movie_id, user_id);
        if (old_score == null) {
            // 该用户未评分
            scoreDao.insertScore(movie_id, user_id, score);
            // 更新电影评分
            movie.setTotal_score(movie.getTotal_score() + score);
            movie.setNum_score(movie.getNum_score() + 1);
            movieDao.updateMovie(movie);
        } else {
            // 该用户已评分
            scoreDao.updateScore(movie_id, user_id, score);
            // 更新电影评分
            movie.setTotal_score(movie.getTotal_score() - old_score.getScore() + score);
            movieDao.updateMovie(movie);
        }

    }
}
